package com.zhou.demo.zhihn;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.BitmapRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.model.GlideUrl;
import com.zhou.demo.callback.LaunchCallback;
import com.zhou.demo.interfaceclass.LauchConnect;
import com.zhou.demo.url.ZhihuUrl;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

public class MainActivity extends AppCompatActivity implements LaunchCallback {
    @ViewInject(R.id.imageView)
    private ImageView imageView;
    private BitmapRequestBuilder<GlideUrl, Bitmap> requestBuilder;
    private LauchConnect lauchConnect;
    private String img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        x.view().inject(this);
        requestBuilder = Glide.with(this)
                .from(GlideUrl.class)
                .asBitmap()
                .dontAnimate()
                .centerCrop()
                .error(R.mipmap.ic_launcher)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        lauchConnect  = new LauchConnect();
        lauchConnect.getLaunchImage(this);

        new Handler().postDelayed(new Runnable() {
            public void run() {
                /* Create an Intent that will start the MainActivity. */
                Intent mainIntent = new Intent(MainActivity.this, ZhiHuNewLatestActivity.class);
                startActivity(mainIntent);
                finish();
            }
        }, 5000); //2900 for release


    }


    @Override
    public void OnLaunchSuccess(String success) {
        Log.e("tag",success);
        try {
            JSONObject jsonObject = new JSONObject(success);
            String text = jsonObject.optString("text");
            img = jsonObject.optString("img");
            x.image().bind(imageView,img);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void OnLaunchFail(String fail) {

    }



}

